<template>
  <div class="regulatory-page">
    <div class="return" @click="goBack">
      <Icon size="25" color="#000000" name="arrow-left" class="back" />
      <div class="title">
        <p class="text">评价管理</p>
      </div>
    </div>
    <div v-for="review in reviews" :key="review.reviewId" class="review-container">
      <router-link :to="{ name: 'PetView', params: { id: review.productId } }" class="card-link">
        <div class="review">
          <div class="review-header">
            <VanImage
                round
                width="3rem"
                height="3rem"
                :src="require('@/assets/backgrounds/user.jpg')"
            />
            <div class="info">
              <p class="review-name">{{ review.username }}</p>
              <p class="review-date">{{ review.createTime }}</p>
            </div>
            <div class="rating">
              <Rate v-model="review.rating" readonly size="13" />
            </div>
          </div>
          <div class="review-text">{{ review.comment }}</div>
        </div>
      </router-link>
      <button @click="deleteReview(review.reviewId, $event)" class="delete-button">删除</button>
    </div>
  </div>
</template>
<script>
import {Icon, Image as VanImage, Rate, Toast} from 'vant';
import http from "@/plugins/http";

export default {
  name: "MyReviews",
  components: { Icon, VanImage, Rate },
  data() {
    return {
      reviews: []
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    fetchReviews(username) {
      http.get(`/review/user/${username}`)
          .then(response => {
            this.reviews = response.data || [];
          })
          .catch(error => {
            console.error("获取评价信息出错:", error);
          });
    },
    deleteReview(reviewId, event) {
      event.preventDefault();
      event.stopPropagation();
      http.delete(`/review/delete/${reviewId}`)
          .then(response => {
              // 更新前端的评价列表，过滤掉被删除的评价
              this.reviews = this.reviews.filter(review => review.reviewId !== reviewId);
              Toast.success("删除成功");
              console.log("1",response)
          })
          .catch(error => {
            console.error("删除评价出错:", error);
          });
    }
  },
  created() {
    const storedUser = JSON.parse(sessionStorage.getItem("username"));
    if (storedUser) {
      this.fetchReviews(storedUser);
    } else {
      console.error("没有用户登录");
    }
  }
};
</script>
<style scoped>
.regulatory-page {
  background-color: #F6F7F9;
  height: 100vh;
}

.return {
  background-color: #FAEAEA;
  padding: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin-bottom: 10px;
}

.back {
  position: absolute;
  left: 10px;
  text-decoration: none;
}

.title {
  display: flex;
  align-items: center;
}

.text {
  margin: 0;
  color: #000000;
  font-size: 16px;
}

.review-container {
  position: relative;
  margin-bottom: 10px;
  padding-bottom: 10px;
}

.review {
  padding: 10px;
  border-radius: 20px;
  background-color: white;
  margin-left: 10px;
  margin-right: 10px;
  cursor: pointer;
  padding-bottom: 40px;
}

.review-header {
  display: flex;
  align-items: flex-end;
  margin-bottom: 10px;
}

.info {
  margin-left: 10px;
  display: flex;
  flex-direction: column;
}

.review-name {
  font-weight: bold;
  margin: 0;
  font-size: 18px;
}

.review-date {
  color: #999;
  margin: 0;
}

.review-text {
  margin-top: 10px;
}

.rating {
  padding-left: 20px;
  padding-bottom: 23px;
}

.card-link {
  text-decoration: none;
  color: inherit;
}

.delete-button {
  border: 1px solid #FA6408;
  color: #FA6408;
  background-color: white;
  border-radius: 10px;
  padding: 5px 10px;
  cursor: pointer;
  position: absolute;
  bottom: 15px;
  right: 20px;
}
</style>
